import Taddress from '@/components/threeAddress.vue'
export default {
  components: {
    Taddress,
  },
  data() {
    return {
      active: '',
      tabList: [{
        name: '家',
        id: 1,
      },
      {
        name: '公司',
        id: 2
      }
      ],
      showdz: false,
      taddress: [],
      tabs: '',
      checkedValue: '',
      area: '',
      city: 0,
      province: 0,
      activetrue: false,//switch初始值
      labelValue: 0,//标签值
    }
  },
  created() {

  },
  onLoad(option) {
    let list = JSON.parse(option.addressid)
    this.taddress = list
    this.active = list.address_label
    this.area = this.taddress.district
    this.city = this.taddress.city
    this.province = this.taddress.province//省
    this.checkedValue = Number(list.isdefault) //是否默认（0false、不默认 1、true默认）
    if (this.checkedValue === 1) {
      this.activetrue = true
    } else {
      this.activetrue = false
    }
  },
  methods: {
    input(){
		  if (this.taddress.mobile.trim().length !== 11) return false
		  // console.log(this.mobile)
		  if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.taddress.mobile))) {
			  this.$u.toast('请输入正确的手机号')
		  }
    },
    change(status) {
      // console.log(status)
      this.checkedValue = status

      // console.log(status)
    },
    tab(id) {
      // 单选
      if (id !== this.active) {
        this.active = id
      } else {
        this.active = -1
        id = '0'
      }

      this.active = id
    },
    confirmdz(e) {
      this.area = Number(e.area.value)
      this.city = e.city.id
      this.province = e.province.id
      this.taddress.mcdistrict = e.area.label
      this.taddress.mccity = e.city.label
      this.taddress.mcprovince = e.province.label
      this.showdz = false
    },
    close() {
      this.showdz = false
    },
    //编辑
    async keepData() {
      if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.taddress.mobile))){
			  this.$u.toast('请输入正确的手机号')
			  return false
      }
      let data = {
        'name': this.taddress.name,
        'mobile': this.taddress.mobile,
        'address': this.taddress.address,
        'province': this.province,
        'city': this.city,
        'district': Number(this.area),
        'address_label': Number(this.active),
        'isdefault': this.checkedValue,
        'id': this.taddress.id
      }
      const res = await this.$api.common.editAddressList(data)
      this.$u.toast('修改成功')
      setTimeout(res => {
        uni.$emit('addressChange', true)
        uni.navigateBack({
          delta: 1
        })
      }, 1000)
    },
    //删除地址
    async delAddress() {
      try {
        let data = {
          'id': this.taddress.id
        }
        const res = await this.$api.common.delAddressList(data)
        this.$u.toast('删除成功')

        setTimeout(res => {
          uni.navigateBack({
            delta: 1
          })
        }, 1000)

      } catch (e) {
        console.log(e)
      }
    }
  }
}
